<?php
 /**
  * _RBNS_ERROR_LOG Write error to logfile
  */  
 define('_RBNS_ERROR_LOG',1);	
 /**
  * _RBNS_ERROR_LOG  Write error to Standart Out and Log file
  */  
 define('_RBNS_ERROR_STOUT',2);
 
 /**
  * _RBNS_ERROR_LOG Write error to Standart Out and Log file
  */  
 define('_RBNS_ERROR_STOUTANDLOG',3);	

 $_RBNS_VERSION_OF_TEMPLATE='';

/****
*   need for back compatible
* 
* */
 define('RBNS_CRITICAL_ERROR','critical'); define('RBNS_SYSTEM_ERROR','system'); define('RBNS_RETURN_ERROR','return'); define('RBNS_USER_ERROR','users');
//
// RBNS main  class
// template parser

class RBNS{

const RBNS_CRITICAL_ERROR='critical';
const RBNS_SYSTEM_ERROR='system';
const RBNS_RETURN_ERROR='return';
const RBNS_USER_ERROR='users';

var $dateInformat='dmY';
var $dateToformat='Ymd';
var $error=''; //errors buffer

function __construct(){
    
	if(!defined('_RBNS_LOCALE')) die('<br/><font color=red>_RBNS_LOCALE is not defined!</font>');
    if(!defined('_RBNS_PARSERPATH')) die('<br/><font color=red>_RBNS_PARSERPATH is not defined!</font>');
	self::defineConstant();

}



/**
 * Show warning
 *
 * @param string $str  Warning string
 * @param number $level 1,2,3 level of warning
 * @return boolean true if warning was show and else otherwise
 */
public static function parse_warning($str,$level=1){

    switch ($level)
    {
      case 3:       if( !(defined('_RBNS_THIRDLEVELWARNING') && _RBNS_THIRDLEVELWARNING) ) return false;
        break;
      case 2:       if( !(defined('_RBNS_SECONDLEVELWARNING') && _RBNS_SECONDLEVELWARNING) ) return false;
        break;
      case 1:
      default:      if( !(defined('_RBNS_FIRSTLEVELWARNING') && _RBNS_FIRSTLEVELWARNING) ) return false;
    };
    echo ( (defined('_RBNS_WARN_LEFTBRAKE'))?_RBNS_WARN_LEFTBRAKE:'').$str.( (defined('_RBNS_WARN_RIGHTBRAKE'))?_RBNS_WARN_RIGHTBRAKE:'' );
    return true;
}

/**
 * Write $error to stream which defined $errortype. If $addtext preset it add to end to message.
 * If $error is present if $GLOBALS['errorsArray'] then $error=$GLOBALS['errorsArray']; it need for localization
 *
 * @param string $error
 * @param enum $errortype RBNS_CRITICAL_ERROR,RBNS_SYSTEM_ERROR,RBNS_RETURN_ERROR,RBNS_USER_ERROR
 * @param striing $addtext
 * @return null
 */
public static  function errorstream($error,$errortype='users',$addtext=''){
	global $errorsArray;

	if(!$errorstext=$errorsArray[$error]){
		self::parse_warning( "Warning! \$errorsArray[$error] is not determine for locale "._RBNS_LOCALE,3);		$errorstext=$error;	};
	if(!empty($addtext)){
		$errorstext=$errorstext." ".$addtext;	}
    switch ($errortype){
       case self::RBNS_CRITICAL_ERROR:
            self::_showerror($errorstext,_RBNS_ERROR_CRITICAL_STREAM);
            die();
        break;
        case self::RBNS_SYSTEM_ERROR:
             self::_showerror($errorstext,_RBNS_ERROR_SYSTEM_STREAM);
        break;
        case self::RBNS_RETURN_ERROR:
            return self::_showerror($errorstext,'return');
        break;
        case self::RBNS_USER_ERROR:
        default:
            self::_showerror($errorstext,_RBNS_ERROR_USERS_STREAM);
    };


}

protected static function _showerror($errorstext,$stream){

	
    if (($stream==_RBNS_ERROR_STOUTANDLOG)||($stream==_RBNS_ERROR_LOG))
    {//output in log
        $fn=_RBNS_ERROR_LOGS_FILENAME;
        

        $fp = fopen ($fn,"a");
        $today = getdate();
        $date = $today['year']."-".$today['month']."-".$today['mday']." "."$today[hours]:$today[minutes]:$today[seconds]" ;
		
       		
        $errorstext="[ $date ] $errorstext class=".__CLASS__."\r\n";
        fwrite($fp,$errorstext);
        fclose($fp);
    };

    if($stream=="return") return  $errorstext;
    if(empty($stream)||($stream!=_RBNS_ERROR_LOG)||($stream!="return"))
        echo ( (defined('_RBNS_ERROR_LEFTBRAKE'))?_RBNS_ERROR_LEFTBRAKE:'' ) . $errorstext. ( (defined('_RBNS_ERROR_RIGHTBRAKE'))?_RBNS_ERROR_RIGHTBRAKE:'' );

}

/**
 * Make complied template file name.
 *
 * @param string $classname
 * @param string $method
 * @param string $versia
 * @return string
 */
protected function makeTemplateFileName($classname,$method,$versia){

    if(!is_dir(RBNS_COMPILED_TEMPLATES_DIR)) $this->errorstream("RBNS_COMPILED_TEMPLATES_DIR IS Not ACCESSABLE",'system');
// try changes $fn=RBNS_COMPILED_TEMPLATES_DIR.strtolower($classname."_".$method."_".$versia).".php5";
//    to strlower gone
 //   $fn=RBNS_COMPILED_TEMPLATES_DIR.($classname."_".$method."_".$versia).".php5";

    return $fn;
}



private function setDateFormat($dmY){
	if(strlen($dmY)!=3) return false;
	$dmy=strtolower($dmY);
	
	if(strpos($dmy,'d')===false) return false;
	if(strpos($dmy,'m')===false) return false;
	if(strpos($dmy,'y')===false) return false;
	return $dmY;
}	

function setDateToFormat($dmY){
	
	if($this->setDateFormat($dmY)) $this->dateToformat=$dmY;
	
}

function setDateFromFormat($dmY){
	if($this->setDateFormat($dmY)) $this->dateInformat=$dmY;
}


function DateReformatFromLocal($date){
    return datereformatstr($date,$this->dateInformat,$this->dateToformat);
}

function DateReformatToLocal($date){
    return datereformatstr($date,$this->dateToformat,$this->dateInformat);
}

function setProperty($name,$value){
    if(empty($name)) {  $this->errorstream('WrongPropertyName','critical');  }
    $this->$name=$value;
}

private function defineConstant(){


/**
* RBNS_COMPILED_TEMPLATES_DIR determine where results of compilation templates will save
*
*/ 	
	if(!defined('RBNS_COMPILED_TEMPLATES_DIR')){
 		define('RBNS_COMPILED_TEMPLATES_DIR',"$_SERVER[DOCUMENT_ROOT]/cmpldtmplts/");	
    }

/**
* _RBNS_ERROR_SYSTEM_STREAM Where System errors stream will write.
*
*/
    if(!defined('_RBNS_ERROR_SYSTEM_STREAM')){
	 	define('_RBNS_ERROR_SYSTEM_STREAM',_RBNS_ERROR_LOG);
    }
    
/**
* _RBNS_ERROR_USERS_STREAM Where User errors stream will write.
*
*/     
    if(!defined('_RBNS_ERROR_USERS_STREAM')){
    	define('_RBNS_ERROR_USERS_STREAM',_RBNS_ERROR_STOUT);
    }

/**
* _RBNS_ERROR_USERS_STREAM Where Critical errors stream will write.
*
*/      
    if(!defined('_RBNS_ERROR_CRITICAL_STREAM')) {
    	define('_RBNS_ERROR_CRITICAL_STREAM',_RBNS_ERROR_STOUTANDLOG);
    }

/**
* Left part of html frame for error. By default '<br/><font color=red><h2>ERROR!</h2>'
*
*/      
    if(!defined('_RBNS_ERROR_LEFTBRAKE')){
    	define('_RBNS_ERROR_LEFTBRAKE','<br/><font color=red><h2>ERROR!</h2>');
    }

/**
* Frame for error. By default '</font>'
*
*/     
    if(!defined('_RBNS_ERROR_RIGHTBRAKE')){
    	define('_RBNS_ERROR_RIGHTBRAKE','</font>');
    }

    if(!defined('_RBNS_WARN_LEFTBRAKE')){
    	define('_RBNS_WARN_LEFTBRAKE','<br/><font color=blue>');
    }

/**
*  Html frame for RBNS Warning Message. By default '</font>'
*
*/      
    if(!defined('_RBNS_WARN_RIGHTBRAKE')){
    	define('_RBNS_WARN_RIGHTBRAKE','</font>');
    }

    if(!defined("_RBNS_ERROR_LOGS_FILENAME")){
		define('_RBNS_ERROR_LOGS_FILENAME',"$_SERVER[DOCUMENT_ROOT]/RBNSerror.log");
    }

/**
* Boolean. Need show warning for level 3. Default - false.
*
*/ 	     
    if(!defined('_RBNS_THIRDLEVELWARNING')){ 
    	define('_RBNS_THIRDLEVELWARNING',false);
    }	

/**
* Boolean. Need show warning for level 2. Default - false.
*
*/    
    if(!defined('_RBNS_SECONDLEVELWARNING')){
    	define('_RBNS_SECONDLEVELWARNING',false);
    }

/**
* Boolean. Need show warning for level 1. Default - true.
*
*/ 	    
    
    if(!defined('_RBNS_FIRSTLEVELWARNING')){
    	define('_RBNS_FIRSTLEVELWARNING',true);
    }

}
};
?>